<?php
//$moduleid = 30;
include '../../common.inc.php';
$dbSqlite = new PDO('sqlite:d:/data/keche.db3');
$ds       = $dbSqlite->query('select count(*) as num from content');
$fo       = $ds->fetchObject();
$num      = intval($fo->num);

if (!isset($start)) {
	$start = 0;
} else {
	if ($start > $num) {
		die(date('Y-m-d H:i:s'));
	}
}
$sql    = "select * from content where ID > $start limit 10";
$result = $dbSqlite->query($sql);
$rs     = $result->fetchAll();
/*
	[ID] => 280
	[已采] => 1
	[已发] => 0
	/[name1] => 宇通ZK6146HSB 双层客车
                                                          暖风系统：电喷水暖除霜系统。视听系统：硬盘播放器、下层2台15寸固定液晶、上层3台17寸电动翻转液晶。其他配置：欧Ⅲ排放标准，电控液压散热风扇，进口CAN总线。
	[pinpai] => 宇 通
*/
//var_dump($rs);
foreach ($rs as $k => $r) {
	$start = $r['ID'];
	$name       = $r['name1'];
	$year       = handleYear(intval($r['面世时间']));
	$ids        = getMatchedids();
	$arbid      = $ids[0];
	$armid      = $ids[1];
	$arcid      = $ids[2];
	$acid       = 446; // 客车
	$price      = handlePrice($r['产品价格']);
	$enginetype = '';
	$emission   = emission($r['发动机环保标准']);
	$gearbox    = '4314';
	$issale     = 1;
	$insert     = "insert into xd_auto_model(
		name, year, arbid, armid, arcid, acid, price, enginetype, emission, gearbox, issale, modeltype,country, listorder
		) values (
		'{$name}',
		'{$year}',
		'{$arbid}',
		'{$armid}',
		'{$arcid}',
		'{$acid}',
		'{$price}',
		'{$enginetype}',
		'{$emission}',
		'{$gearbox}',
		'{$issale}',
		'commercial',
		0,
		20
		)";

	$db->query($insert);
	$iid = $db->insertId();

	$arr                         = array();
	$arr['amid']                 = $iid;
	$arr['note']                 = $r['描述'];
	$arr['seat']                 = $r['座位数'];
	$arr['horsepower']           = $r['马力'];
	$lwh                         = explode('*', $r['长宽高']);
	$arr['body_long']            = $lwh[0];
	$arr['body_width']           = $lwh[1];
	$arr['body_height']          = $lwh[2];
	$arr['wheelbase']            = intval($r['轴距']);
	$arr['tread']                = $r['前轮距后轮距'];
	$arr['rear_suspension']      = $r['前悬后悬'];
	$arr['auto_weight']          = intval($r['整备质量']);
	$arr['max_weight']           = intval($r['最大总质量']);
	$arr['max_speed']            = intval($r['最高车速']);
	$arr['maximum_gradeability'] = $r['最大爬坡度'];
	$arr['turning_radius']       = intval($r['最小转弯直径']);
	$arr['min_ground_clearance'] = intval($r['最小离地间隙']);
	$arr['max_inner_height']     = intval($r['车内最大高度']);
	$arr['luggage_volume']       = $r['行李仓容积'];
	$arr['engine_model']         = $r['发动机型号'];
	$arr['engine_type']          = $r['发动机型式'];
	$arr['engine_position']      = $r['发动机位置'];
	$arr['emissions']            = $r['发动机排量'];
	$arr['power']                = $r['发动机最大输出功率'];
	$arr['torque']               = $r['发动机最大扭矩'];
	$arr['fuel_consumption']     = $r['发动机油耗'];
	$arr['engine_oil_volume']    = $r['发动机油箱容积'];
	$arr['chassie_type']         = $r['底盘型号'];
	$arr['chassie_clutch']       = $r['底盘离合器'];
	$arr['chassie_gear']         = $r['底盘变速器'];
	$arr['chassie_axles']        = $r['底盘车桥'];
	$arr['chassis_brake']        = $r['底盘制动系统'];
	$arr['chassis_steer']        = $r['底盘转向系统'];
	$arr['chassis_suspension']   = $r['底盘悬架系统'];
	$arr['chassis_retarder']     = $r['底盘缓速器'];
	$arr['chassis_tire']         = $r['底盘轮胎规格'];
	$arr['air_conditioning']     = $r['其他空调类型'];

	$sql = Sql::insert('xd_auto_model_commercial', $arr);
	if (!$db->query($sql)) {
		print_r($sql);exit();
	}
}
echo '<meta http-equiv="refresh" content="0; url='.$_SERVER['PHP_SELF'].'?start='.$start.'" />';
function handleYear($old) {
	$years = PCache::read('autoCategory-year.php');
	foreach ($years as $k => $v) {
		if ($v['name'] == $old) {
			return $k;
		}
	}
	return '0';
}

function getMatchedids() {
	return array(
		4274, 4275, 4276
	);
}

function handlePrice($p) {
	return intval($p) * 10000;
}

function emission($tp) {
	$ge = array('欧IV+OBD', '国IV/国V', '国Ⅰ', '欧Ⅱ', '欧II', '国II', '国Ⅱ', '欧II/欧V', '欧洲2号', '国 Ⅱ', '欧Ⅱ 国II', '欧I', '欧Ⅰ或欧Ⅱ', '达低速货车、三轮车第二阶段排放要求', '欧Ⅰ', '欧Ι');
	if (in_array($tp, $ge)) {
		return 4326;
	}
	$gs = array('国III EGR', '118', '国111', '国III+OBD', '国III/国IV', '欧III/欧IV', '国III非OBD', 'GB18352.2-2001,GB384', '国四', '欧2', 'GB17691-2005国Ⅲ,GB384', 'Euro Ⅲ', '欧 Ⅲ', '国Ⅲ、国Ⅳ', '国Ⅲ 国Ⅳ', '欧III 欧IV', '欧III/国III', 'III', '国Ⅲ/国Ⅳ', '欧II欧III', '3.76', '国3', '殴3', '国Ⅲ/国Ⅳ（SCR）', 'EuroIII', '国III', '国Ⅲ', '欧III', '欧II 欧III', '欧Ⅲ',
		'国II/国III',
		'国Ⅲ/国IV',
		'国Ⅲ/',
		'国 Ⅲ', '国三',
		'国Ⅲ/国II',
		'国Ⅱ、国Ⅲ',
		'国IV/欧IV',
		'国III/欧III', '国Ⅲ、国IV', '欧三', '欧Ⅱ,欧Ⅲ', '欧III（共轨）');
	if (in_array($tp, $gs)) {
		return 519;
	}
	$gsi = array('国IV+OBD', '国Ⅳ+OBD', '国Ⅳ', '国IV', '欧IV', '欧IV，可选欧III', '欧Ⅳ', '国IVI');
	if (in_array($tp, $gsi)) {
		return 520;
	}
	$gw = array('欧II 欧V', '欧V', '欧VI', '欧V（SCR）', '国V', '直列', '欧五');
	if (in_array($tp, $gw)) {
		return 521;
	}

	return 0;
}